package com.qilu.careprogram.controller;

import com.qilu.careprogram.common.AjaxResult;
import com.qilu.careprogram.model.Result;
import com.qilu.careprogram.pojo.dto.QaQueryDTO;
import com.qilu.careprogram.pojo.dto.NurseAnswerDTO;
import com.qilu.careprogram.pojo.dto.WxSubscribeMessageDTO;
import org.springframework.web.bind.annotation.*;
import com.qilu.careprogram.pojo.dto.MessageAlertDTO;
import com.qilu.careprogram.pojo.dto.DoctorAnswerLinkDTO;
import com.qilu.careprogram.pojo.dto.DoctorAnswerDTO;
import com.qilu.careprogram.pojo.vo.DoctorQuestionVO;
import com.qilu.careprogram.service.MessageAlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/messageAlert")
public class MessageAlertController {

    @Autowired
    private MessageAlertService messageAlertService;

    @PostMapping("/getOpenid")
    public AjaxResult getOpenid(@RequestBody MessageAlertDTO messageAlertDTO) {
        try {
            String openid = messageAlertService.getOpenid(messageAlertDTO.getCode(), messageAlertDTO.getHospitalNumber());
            return AjaxResult.success().put("openid", openid);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 发送微信订阅消息
     */
    @PostMapping("/sendMessage")
    public AjaxResult sendSubscribeMessage(@RequestBody WxSubscribeMessageDTO messageDTO) {
        try {
            // 调用Service发送消息，返回微信接口的响应结果
            String result = messageAlertService.sendSubscribeMessage(messageDTO);
            return AjaxResult.success("消息发送成功").put("wxResult", result);
        } catch (Exception e) {
            // 捕获异常并返回错误信息
            return AjaxResult.error("消息发送失败：" + e.getMessage());
        }
    }

    @PostMapping("/addAuthority")
    public AjaxResult addAuthority(@RequestBody MessageAlertDTO messageAlertDTO) {
        try {
            messageAlertService.addAuthority(messageAlertDTO.getOpenid(), messageAlertDTO.getHospitalNumber());
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/historyQa")
    public AjaxResult historyQa(@RequestBody MessageAlertDTO messageAlertDTO) {
        try {
            Map<String, Object> result = messageAlertService.historyQa(messageAlertDTO.getHospitalNumber());
            System.out.println(messageAlertDTO.getHospitalNumber());
            System.out.println(result);
            return AjaxResult.success().put("data", result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询问答记录
     */
    @GetMapping("/selectQa")
    public Result selectQa(QaQueryDTO queryDTO) {
        System.out.println(queryDTO);
        return Result.success(messageAlertService.selectQa(queryDTO));
    }

    /**
     * 护士回答问题
     */
    @PostMapping("/nurseAnswer")
    public Result nurseAnswer(@RequestBody NurseAnswerDTO answerDTO) {
        messageAlertService.nurseAnswer(answerDTO);
        return Result.success("成功提交回答");
    }

    /**
     * 获取医生回答链接
     */
    @PostMapping("/doctorGetAnswerLink")
    public Result<DoctorAnswerLinkDTO> getDoctorAnswerLink(@RequestParam Long id) {
        System.out.println(id);
        try {
            DoctorAnswerLinkDTO linkDTO = messageAlertService.getDoctorAnswerLink(id);
            return Result.success(linkDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 医生获取问题信息
     */
    @GetMapping("/doctorGetQuestion")
    public Result<DoctorQuestionVO> getQuestionForDoctor(
            @RequestParam Long questionId,
            @RequestParam String token) {
        System.out.println(questionId);
        System.out.println(token);
        try {
            DoctorQuestionVO questionVO = messageAlertService.getQuestionForDoctor(questionId, token);
            return Result.success(questionVO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 医生提交回答
     */
    @PostMapping("/doctorAnswer")
    public Result doctorAnswer(@RequestBody DoctorAnswerDTO doctorAnswerDTO) {
        try {
            messageAlertService.doctorAnswer(doctorAnswerDTO);
            return Result.success("回答提交成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}